﻿using System;
using System.Windows.Forms;


namespace 网页填充
{
    public partial class Form1 : Form
    {
        Element el = new Element();

        public Form1()
        {
            InitializeComponent();
        }

        int count;



        private void button1_Click(object sender, EventArgs e)
        {
            //this.webBrowser1.Url = new System.Uri("https://aq.91.com/WebStaticPage/loginforaq.htm", System.UriKind.Absolute);
            //el.Write_value(el.GetElement_Name(webBrowser1, "txtUserName"), "isaced");
           // el.Write_value(el.GetElement_Id(webBrowser1, "txtPassword"), "123456");

          


            //webBrowser1.Document.GetElementById("txtUserName").InnerText = "weiling";
            //this.webBrowser1.Document.All["txtUserName"].SetAttribute("value", "tqnd990866");
            //非框架

            //HtmlElement tbUserid = webBrowser1.Document.Window.Frames[0].Document.All["txtUserName"];
            //tbUserid.SetAttribute("value", "k221");

            //HtmlElement tbPasswd = webBrowser1.Document.Window.Frames[0].Document.All["txtPassword"];
            //tbPasswd.SetAttribute("value", "33311");

            //HtmlDocument htmlDoc = webBrowser1.Document.Window.Frames[0].Document;
            //foreach (HtmlElement h in htmlDoc.All)
            //{
            //    if (h.GetAttribute("type").ToString() == "submit")
            //    {
            //        if (h.GetAttribute("value").ToString() == "确定")
            //        {
            //            h.InvokeMember("click");
            //            break;
            //        }
            //    }
            //}
            //webBrowser1.Document.GetElementById("txtUserName").InnerText = textBox1.ToString().Trim();//fill name

            //webBrowser1.Document.GetElementById("txtPassword").InnerText = textBox1.Text.ToString().Trim();//fill pwd
            //HtmlElement formLogin = webBrowser1.Document.Forms["frmLogin"];
            //formLogin.InvokeMember("submit");

            //Navigate(textBox1.Text);
            //System.Threading.Thread.Sleep(5000);
            //webBrowser1.Document.GetElementById("txtUserName").InnerText = "weiling";
            el.Write_value(el.GetElement_Name(webBrowser1, "txtUserName"), "tqnd990866");
            el.Write_value(el.GetElement_Id(webBrowser1, "txtPassword"), "1");
            el.Btn_click(el.GetElement_Id(webBrowser1, "btnLogin"), "click");
            //System.Threading.Thread.Sleep(5000);
            //Navigate("");
            //Navigate("https://aq.91.com/WebStaticPage/loginforaq.htm");

            this.webBrowser1.Url = new System.Uri("https://aq.91.com/NDUser_ResetPassword.aspx", System.UriKind.Absolute);
            //webBrowser1.Navigate(new Uri("https://aq.91.com/NDUser_ResetPassword.aspx"));

        }

        private void webBrowser1_Navigated(object sender,WebBrowserNavigatedEventArgs e)
        {
            textBox1.Text = webBrowser1.Url.ToString();
        }

        private void Navigate(String address)
        {
            if (String.IsNullOrEmpty(address)) return;
            if (address.Equals("about:blank")) return;
            if (!address.StartsWith("http://") &&
                !address.StartsWith("https://"))
            {
                address = "http://" + address;
            }
            try
            {
                webBrowser1.Navigate(new Uri(address));
            }
            catch (System.UriFormatException)
            {
                return;
            }
        }


        #region
        public class Element
        {

            //根据Name获取元素
            public HtmlElement GetElement_Name(WebBrowser wb, string Name)
            {
                HtmlElement e = wb.Document.All[Name];
                return e;
            }

            //根据Id获取元素
            public HtmlElement GetElement_Id(WebBrowser wb, string id)
            {
                HtmlElement e = wb.Document.GetElementById(id);
                return e;
            }

            //根据Index获取元素
            public HtmlElement GetElement_Index(WebBrowser wb, int index)
            {
                HtmlElement e = wb.Document.All[index];
                return e;
            }

            //获取form表单名name,返回表单
            public HtmlElement GetElement_Form(WebBrowser wb, string form_name)
            {
                HtmlElement e = wb.Document.Forms[form_name];
                return e;
            }


            //设置元素value属性的值
            public void Write_value(HtmlElement e, string value)
            {
                e.SetAttribute("value", value);
            }

            //执行元素的方法，如：click，submit(需Form表单名)等
            public void Btn_click(HtmlElement e, string s)
            {

                e.InvokeMember(s);
            }

        }

        #endregion 声明

        private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            //if (e.Url.ToString().Trim() == strURL.Trim())
            //{
            //    //已经加载完成
            //}  
            //IHTMLDocument2 vDocument = (IHTMLDocument2)webBrowser1.Document.DomDocument;
            //vDocument.parentWindow.execScript("function confirm(str){return true;} ", "javascript"); //弹出确认
            //vDocument.parentWindow.execScript("function alert(str){return true;} ", "javaScript");//弹出提示
            MessageBox.Show("这是e.Url:\n" + e.Url + "\n这是1webBrowser1.Document.Url:\n" + webBrowser1.Document.Url);
            count--;

        }

        private void webPage_Navigated(object sender, WebBrowserNavigatedEventArgs e)
        {
            count++;
            MessageBox.Show("这是e.Url:\n" + e.Url + "\n这是2webBrowser1.Document.Url:\n" + webBrowser1.Document.Url);
        }

        private void Form1_Load_1(object sender, EventArgs e)
        {
            Navigate("https://aq.91.com/WebStaticPage/loginforaq.htm");
        }

        private void button2_Click(object sender, EventArgs e)
        {
            webBrowser1.Navigate(new Uri("https://aq.91.com/NDUser_ResetPassword.aspx"));
        }
    }
}
